<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/xizhongren8980/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/xizhongren8980/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/xizhongren8980/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/xizhongren8980/images/logo.svg" color="#222">

<link rel="stylesheet" href="/xizhongren8980/css/main.css">


<link rel="stylesheet" href="/xizhongren8980/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/xizhongren8980/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="在上篇中大概介绍了 mybatis 的基本增删改查操作，其中就涉及到 parameterType 输入类型为基本类型和pojo（实体类） 对象，而在 mybatis 中是使用 OGNL 表达式来解析对象的值。">
<meta property="og:type" content="article">
<meta property="og:title" content="【MyBatis入门_03】之使用实体类包装对象作为查询条件">
<meta property="og:url" content="https://gitee.com/xizhongren8980/xizhongren8980.git/2020/06/06/%E3%80%90MyBatis%E5%85%A5%E9%97%A8%E3%80%91%E4%B9%8B%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BD%93%E7%B1%BB%E5%8C%85%E8%A3%85%E5%AF%B9%E8%B1%A1%E4%BD%9C%E4%B8%BA%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6/index.html">
<meta property="og:site_name" content="一杯茶一包烟，一个BUG改一天">
<meta property="og:description" content="在上篇中大概介绍了 mybatis 的基本增删改查操作，其中就涉及到 parameterType 输入类型为基本类型和pojo（实体类） 对象，而在 mybatis 中是使用 OGNL 表达式来解析对象的值。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com//xizhongren8980/BlogImages/raw/master/imgs/20200606105607.png">
<meta property="og:image" content="https://gitee.com//xizhongren8980/BlogImages/raw/master/imgs/20200606200438.png">
<meta property="article:published_time" content="2020-06-06T12:11:31.302Z">
<meta property="article:modified_time" content="2020-06-10T05:08:08.683Z">
<meta property="article:author" content="MisterTian">
<meta property="article:tag" content="数据库">
<meta property="article:tag" content="查询">
<meta property="article:tag" content="框架">
<meta property="article:tag" content="mybatis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com//xizhongren8980/BlogImages/raw/master/imgs/20200606105607.png">

<link rel="canonical" href="https://gitee.com/xizhongren8980/xizhongren8980.git/2020/06/06/%E3%80%90MyBatis%E5%85%A5%E9%97%A8%E3%80%91%E4%B9%8B%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BD%93%E7%B1%BB%E5%8C%85%E8%A3%85%E5%AF%B9%E8%B1%A1%E4%BD%9C%E4%B8%BA%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>【MyBatis入门_03】之使用实体类包装对象作为查询条件 | 一杯茶一包烟，一个BUG改一天</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/xizhongren8980/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">一杯茶一包烟，一个BUG改一天</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">收藏永不停止，学习永不开始</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/xizhongren8980/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/xizhongren8980/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/xizhongren8980/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/xizhongren8980/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/xizhongren8980/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/xizhongren8980/xizhongren8980.git/2020/06/06/%E3%80%90MyBatis%E5%85%A5%E9%97%A8%E3%80%91%E4%B9%8B%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BD%93%E7%B1%BB%E5%8C%85%E8%A3%85%E5%AF%B9%E8%B1%A1%E4%BD%9C%E4%B8%BA%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/xizhongren8980/images/head.jpg">
      <meta itemprop="name" content="MisterTian">
      <meta itemprop="description" content="一个小菜鸟">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一杯茶一包烟，一个BUG改一天">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          【MyBatis入门_03】之使用实体类包装对象作为查询条件
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-06-06 20:11:31" itemprop="dateCreated datePublished" datetime="2020-06-06T20:11:31+08:00">2020-06-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-06-10 13:08:08" itemprop="dateModified" datetime="2020-06-10T13:08:08+08:00">2020-06-10</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/xizhongren8980/categories/MyBatis/" itemprop="url" rel="index"><span itemprop="name">MyBatis</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>在上篇中大概介绍了 mybatis 的基本增删改查操作，其中就涉及到 parameterType 输入类型为基本类型和pojo（实体类） 对象，而在 mybatis 中是使用 OGNL 表达式来解析对象的值。<a id="more"></a></p>
<h3 id="OGNL表达式"><a href="#OGNL表达式" class="headerlink" title="OGNL表达式"></a>OGNL表达式</h3><p>即 Object Graphic Navigation Language 对象图导航语言，它是通过对象的取值方法来获取数据。</p>
<p>例如：在获取图书的名称时</p>
<ul>
<li><p>在实体类中的写法是 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">book.getName();</span><br></pre></td></tr></table></figure>
</li>
<li><p>OGNL 表达式写法</p>
<figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">book.<span class="built_in">name</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<p>之所以我们可以直接使用属性 <code>name</code> ，而不使用 <code>book.</code> ，是因为在 parameterType 中已经提供了属性所属的类，在这种情况下我们就不需要写对象名。</p>
<h3 id="实体类包装对象作为查询条件"><a href="#实体类包装对象作为查询条件" class="headerlink" title="实体类包装对象作为查询条件"></a>实体类包装对象作为查询条件</h3><p>在开发中，查询条件往往是比较复杂的，例如还有图书出售的订单信息，这个时候只传入一个Book对象就不行了，所以我们要根据查询条件，自定义一个新的pojo类包含所有的查询条件作为参数传入。</p>
<h4 id="自定义包装类型-pojo"><a href="#自定义包装类型-pojo" class="headerlink" title="自定义包装类型 pojo"></a>自定义包装类型 pojo</h4><ul>
<li><p>定义一个简单的图书类包装类 BookPojo，它只包含一个Book类的属性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: BookPojo</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Auther</span>: 戏中人</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span>: 2020/6/6 10:29</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>:图书类的简单包装对象</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookPojo</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Book book;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Book <span class="title">getBook</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> book;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setBook</span><span class="params">(Book book)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.book = book;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>定义 dao 接口方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**使用包装类对象作为查询条件</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@Param</span> []</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@return</span> java.util.List&lt;com.xizhongren.domain.Book&gt;</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="function">List&lt;Book&gt; <span class="title">selectBookByBookPojo</span><span class="params">(BookPojo bookPojo)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>配置映射文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--使用包装类作为查询条件--&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"selectBookByBookPojo"</span> <span class="attr">parameterType</span>=<span class="string">"com.xizhongren.domain.BookPojo"</span> <span class="attr">resultType</span>=<span class="string">"com.xizhongren.domain.Book"</span>&gt;</span></span><br><span class="line">       select * from t_book where name like '%$&#123;book.name&#125;%';</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>可以看出，在sql语句中的条件要写为 book.name 才行，而不是之前的 name。</p>
</li>
<li><p>编写测试方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSelectBookByBookPojo</span><span class="params">()</span> </span>&#123;</span><br><span class="line">       BookPojo bookPojo = <span class="keyword">new</span> BookPojo();</span><br><span class="line">       Book book = <span class="keyword">new</span> Book();</span><br><span class="line">       book.setName(<span class="string">"数据"</span>);</span><br><span class="line">       bookPojo.setBook(book);</span><br><span class="line">       List&lt;Book&gt; book2 = bookDao.selectBookByBookPojo(bookPojo);</span><br><span class="line">       <span class="keyword">for</span> (Book book1 : book2) &#123;</span><br><span class="line">           System.out.println(book1);</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>输出完全正确</p>
<p><img src="https://gitee.com//xizhongren8980/BlogImages/raw/master/imgs/20200606105607.png" alt="image-20200606105606020"></p>
</li>
</ul>
<p>到这里基本就完成了实体类参数作查询条件。</p>
<h3 id="解决实体类属性和数据库表列名不匹配的情况"><a href="#解决实体类属性和数据库表列名不匹配的情况" class="headerlink" title="解决实体类属性和数据库表列名不匹配的情况"></a>解决实体类属性和数据库表列名不匹配的情况</h3><p>在之前所有的查询或者其他各种操作中，我们都是采用实体类的属性名严格与数据库列名一一对应完全相同的方式，如果我们尝试修改为不同，那么肯定是不能达到预期的查询效果的。</p>
<p>例如我把 Book 类的属性值都全部加一个 book</p>
<p>原来的Book类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Book</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Integer id;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> BigDecimal price;</span><br><span class="line">    <span class="keyword">private</span> String author;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p>修改后的 Book 类（为了修改后恢复方便，我就新写一个 Book_test 类来测试）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Book_test</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Integer bookId;</span><br><span class="line">    <span class="keyword">private</span> String bookName;</span><br><span class="line">    <span class="keyword">private</span> String bookAuthor;</span><br><span class="line">    <span class="keyword">private</span> BigDecimal bookPrice;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果现在想要还要能正确运行的话，就需要配置实体类属性名和数据库表列名之间的映射关系了。基本步骤如下：</p>
<ul>
<li><p>配置查询结果的列名和实体类的属性名对应关系</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"bookMap"</span> <span class="attr">type</span>=<span class="string">"com.xizhongren.domain.Book_test"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--先配置主键字段的对应关系--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">"bookId"</span> <span class="attr">column</span>=<span class="string">"id"</span>&gt;</span><span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--非主键字段的对应关系--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"bookName"</span> <span class="attr">column</span>=<span class="string">"name"</span>&gt;</span><span class="tag">&lt;/<span class="name">result</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"bookPrice"</span> <span class="attr">column</span>=<span class="string">"price"</span>&gt;</span><span class="tag">&lt;/<span class="name">result</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"bookAuthor"</span> <span class="attr">column</span>=<span class="string">"author"</span>&gt;</span><span class="tag">&lt;/<span class="name">result</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>修改查询所有图书的映射文件</p>
<p>这是修改前的</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--id 表示的是接口中具体的方法，resultType表示的是查询返回结果封装对象的类型--&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findAll"</span> <span class="attr">resultType</span>=<span class="string">"com.xizhongren.domain.Book"</span>&gt;</span></span><br><span class="line">       select * from t_book;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>修改后的</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--id 表示的是接口中具体的方法，resultType表示的是查询返回结果封装对象的类型--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findAll"</span> <span class="attr">resultMap</span>=<span class="string">"bookMap"</span>&gt;</span></span><br><span class="line">        select * from t_book;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>只要修改一下返回类型即可。</p>
</li>
<li><p>运行测试代码查看结果</p>
<p><img src="https://gitee.com//xizhongren8980/BlogImages/raw/master/imgs/20200606200438.png" alt="image-20200606200435882"></p>
<p>结果仍然完全正确。</p>
</li>
</ul>
<p>这种方法可以有效解决属性名和列名不匹配带来的问题，当然这不是唯一的解决办法（例如还可以使用在sql语句中起别名的用法，但是这会导致修改量很大），但是却是一个值得使用的办法。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/xizhongren8980/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="tag"># 数据库</a>
              <a href="/xizhongren8980/tags/%E6%9F%A5%E8%AF%A2/" rel="tag"># 查询</a>
              <a href="/xizhongren8980/tags/%E6%A1%86%E6%9E%B6/" rel="tag"># 框架</a>
              <a href="/xizhongren8980/tags/mybatis/" rel="tag"># mybatis</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/xizhongren8980/2020/06/06/%E3%80%90MyBatis%E5%85%A5%E9%97%A8%E3%80%91%E4%B9%8B%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5%E6%93%8D%E4%BD%9C/" rel="prev" title="【MyBatis入门_02】之增删改查操作">
      <i class="fa fa-chevron-left"></i> 【MyBatis入门_02】之增删改查操作
    </a></div>
      <div class="post-nav-item">
    <a href="/xizhongren8980/2020/06/06/%E3%80%90MyBatis%E5%85%A5%E9%97%A8%E3%80%91%E4%B9%8B-SqlMapConfig.xml-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%B0%8F%E7%BB%93/" rel="next" title="【MyBatis入门_04】之 SqlMapConfig.xml 配置文件小结">
      【MyBatis入门_04】之 SqlMapConfig.xml 配置文件小结 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#OGNL表达式"><span class="nav-number">1.</span> <span class="nav-text">OGNL表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实体类包装对象作为查询条件"><span class="nav-number">2.</span> <span class="nav-text">实体类包装对象作为查询条件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#自定义包装类型-pojo"><span class="nav-number">2.1.</span> <span class="nav-text">自定义包装类型 pojo</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#解决实体类属性和数据库表列名不匹配的情况"><span class="nav-number">3.</span> <span class="nav-text">解决实体类属性和数据库表列名不匹配的情况</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="MisterTian"
      src="/xizhongren8980/images/head.jpg">
  <p class="site-author-name" itemprop="name">MisterTian</p>
  <div class="site-description" itemprop="description">一个小菜鸟</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/xizhongren8980/archives/">
        
          <span class="site-state-item-count">57</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/xizhongren8980/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/xizhongren8980/tags/">
          
        <span class="site-state-item-count">101</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://mistertian8980.github.io/" title="https:&#x2F;&#x2F;mistertian8980.github.io&#x2F;" rel="noopener" target="_blank">Github博客</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">MisterTian</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/xizhongren8980/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/xizhongren8980/lib/anime.min.js"></script>
  <script src="/xizhongren8980/lib/velocity/velocity.min.js"></script>
  <script src="/xizhongren8980/lib/velocity/velocity.ui.min.js"></script>

<script src="/xizhongren8980/js/utils.js"></script>

<script src="/xizhongren8980/js/motion.js"></script>


<script src="/xizhongren8980/js/schemes/pisces.js"></script>


<script src="/xizhongren8980/js/next-boot.js"></script>


  <script defer src="//cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/three.min.js"></script>
    <script defer src="//cdn.jsdelivr.net/gh/theme-next/theme-next-three@1/canvas_lines.min.js"></script>


  




  
<script src="/xizhongren8980/js/local-search.js"></script>













  

  

</body>
</html>
